package org.learning;

//Thread.interrupt() 的精确含义是“唤醒轻量级阻塞”,只对WAITING和TIMED_WAITING状态的线程起作用，而不是字面意思“中断一个线程”
public class A_05 {
    private static class MyThread extends Thread {
        @Override
        public void run() {
            while (true) {
                boolean isInterrupted = Thread.currentThread().isInterrupted();
                System.out.println("MyThread status is interrupted:" + isInterrupted);
            }
        }
    }

    private static class MyThread2 extends Thread {
        @Override
        public void run() {
            try {
                System.out.println("MyThread2 status is interrupted:" + Thread.currentThread().isInterrupted());
                sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("MyThread2 status is interrupted:" + Thread.currentThread().isInterrupted());
                e.printStackTrace();
            }
        }
    }

    private static class MyThread3 extends Thread {
        @Override
        public void run() {
            int i = 0;
            while (true) {
                boolean interrupted = isInterrupted();
                System.out.println("MyThread3 status is interrupted:" + interrupted);
                ++i;
                if (i > 200) {
                    interrupted = Thread.interrupted();
                    System.out.println("MyThread3 status is interrupted:" + interrupted);
                    interrupted = Thread.interrupted();
                    System.out.println("MyThread3 status is interrupted:" + interrupted);
                    interrupted = isInterrupted();
                    System.out.println("MyThread3 status is interrupted:" + interrupted);
                    break;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        // MyThread myThread = new MyThread();
        // myThread.start();
        MyThread2 myThread2 = new MyThread2();
        myThread2.start();

        Thread.sleep(20);
        // myThread.interrupt();
        myThread2.interrupt();
        Thread.sleep(1000);
        System.exit(0);
    }
}
